﻿using Crm.CrmClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Crm.Views
{
    public partial class RaporParametre : ChildWindow
    {
        public Rapor Rapor { get; set; }

        public RaporParametre()
        {
            InitializeComponent();
            this.Loaded += RaporParametre_Loaded;
        }

        private string FindControlByGetValue(string sql)
        {
            string sonuc = sql;
            foreach (var q in lstParametre.Children)
            {
                if (q.GetType() == typeof(StackPanel))
                {
                    StackPanel st = q as StackPanel;
                    if (st.Tag != null)
                    {
                        string prm = st.Tag.ToString();
                        string val = "";
                        foreach (var c in st.Children)
                        {
                            if (c.GetType() == typeof(DatePicker))
                            {
                                DatePicker dte = c as DatePicker;
                                if (dte.SelectedDate.HasValue)
                                {
                                    DateTime tr = dte.SelectedDate.Value;
                                    val = string.Format("{0}-{1}-{2}", tr.Year, tr.Month, tr.Day);
                                }
                                break;
                            }
                            else if (c.GetType() == typeof(TextBox))
                            {
                                TextBox txt = c as TextBox;
                                if (!string.IsNullOrEmpty(txt.Text))
                                {
                                    val = txt.Text;
                                }
                                break;
                            }
                        }
                        sonuc = sonuc.Replace(prm, val);
                    }
                }
            }
            return sonuc;
        }

        void RaporParametre_Loaded(object sender, RoutedEventArgs e)
        {
            if (Rapor.Tipi != 1)
            {
                if (Rapor.GridSorgu.ToLower().IndexOf("where") > -1)
                {
                    StackPanel panel = new StackPanel();
                    panel.Orientation = Orientation.Horizontal;
                    panel.Height = 30;
                    TextBlock txt = new TextBlock();
                    txt.FontSize = 14;
                    txt.Text = "Grid Rapor Parametreleri";
                    txt.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                    txt.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    panel.Children.Add(txt);
                    panel.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                    panel.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    lstParametre.Children.Add(panel);
                    ParametreDuzenle(Rapor.GridSorgu);
                }
            }

            if (Rapor.Tipi != 0)
            {
                if (Rapor.ChartSorgu.ToLower().IndexOf("where") > -1)
                {
                    StackPanel panel = new StackPanel();
                    panel.Orientation = Orientation.Horizontal;
                    panel.Height = 30;
                    TextBlock txt = new TextBlock();
                    txt.FontSize = 14;
                    txt.Text = "Grafik Rapor Parametreleri";
                    txt.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                    txt.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
                    panel.Children.Add(txt);
                    panel.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                    panel.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    lstParametre.Children.Add(panel);

                    ParametreDuzenle(Rapor.ChartSorgu);
                }
            }

        }

        private void ParametreDuzenle(string query)
        {
            if (query.ToLower().IndexOf("where") > -1)
            {
                string[] where = query.Substring(query.ToLower().IndexOf("where")).Split(new string[] { "and", "AND" }, StringSplitOptions.None);
                StackPanel panel;
                TextBlock txt;
                TextBox txt2;
                DatePicker dte;
                foreach (string p in where)
                {
                    if (p.IndexOf('@') > -1)
                    {
                        panel = new StackPanel();
                        panel.Orientation = Orientation.Horizontal;
                        panel.Height = 30;
                        string[] alanlar = p.Split('@');
                        panel.Tag = "@" + alanlar[1] + "@" + alanlar[2] + "@";

                        if (alanlar[1] == "D")
                        {
                            dte = new DatePicker();
                            dte.Width = 150;
                            dte.Margin = new Thickness(1, 1, 1, 1);
                            dte.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
                            dte.VerticalAlignment = System.Windows.VerticalAlignment.Center;

                            txt = new TextBlock();
                            txt.Width = 200;
                            txt.Text = alanlar[2];
                            txt.Margin = new Thickness(1, 1, 1, 1);
                            txt.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                            txt.VerticalAlignment = System.Windows.VerticalAlignment.Center;

                            panel.Children.Add(txt);
                            panel.Children.Add(dte);

                            lstParametre.Children.Add(panel);
                        }
                        else
                        {
                            txt2 = new TextBox();
                            txt2.Width = 150;
                            txt2.Margin = new Thickness(1, 1, 1, 1);
                            txt2.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
                            txt2.VerticalAlignment = System.Windows.VerticalAlignment.Center;

                            txt = new TextBlock();
                            txt.Width = 200;
                            txt.Text = alanlar[2];
                            txt.Margin = new Thickness(1, 1, 1, 1);
                            txt.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                            txt.VerticalAlignment = System.Windows.VerticalAlignment.Center;

                            panel.Children.Add(txt);
                            panel.Children.Add(txt2);

                            lstParametre.Children.Add(panel);
                        }
                    }

                }
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (Rapor.Tipi != 1)
            {
                Rapor.GridSorgu = FindControlByGetValue(Rapor.GridSorgu);
            }
            else
            {
                Rapor.GridSorgu = "";
            }

            if (Rapor.Tipi != 1)
            {
                Rapor.ChartSorgu = FindControlByGetValue(Rapor.ChartSorgu);
            }
            else
            {
                Rapor.ChartSorgu = "";
            }

            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

